/* $RCSFile: MediaPlayerLibEdit.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/MediaPlayerLib/MediaPlayerLibEdit.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:21  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:56  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.UI;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Settings.PageModules.MediaPlayerLib;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.MediaPlayerLib
{
    public partial class MediaPlayerLibEdit : AddEditItemPage
    {
		protected AddonNice.UI.WebControls.IHtmlEditor DesktopText;

		MediaPlayerLibDB MediaPlayerUrlDB = new MediaPlayerLibDB();

        string _UploadDirectory =   string.Empty;
        string UploadDirectory
        {
            get
            {
                if ( _UploadDirectory != string.Empty )
                    return _UploadDirectory;
                _UploadDirectory        =   portalSettings.DefaultMediaVirtualPath;
                if ( !_UploadDirectory.EndsWith("/") )
                    _UploadDirectory    +=   "/";
               return _UploadDirectory;
            }
        }
		
		public MediaPlayerLibEdit() : base(true,true,true,true)
        {}

        /// <summary>
        /// The Page_Load event on this Page is used to obtain the ModuleID
        /// and ItemID of the MediaPlayerUrl to edit.
        /// It then uses the AddonNice.MediaPlayerLibDB() data component
        /// to populate the page's edit controls with the annoucement details.
        /// </summary>
        protected void Page_Load(object sender, System.EventArgs e) 
        {
			if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit Page_Load isPostBack: {0}.",IsPostBack),PageTraceSwitch.Sw.Info);
            //install editor
            DesktopText					            =   GetEditor(PlaceHolderHTMLEditor);
			if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit Page_Load DesktopText is: {0}.",DesktopText),PageTraceSwitch.Sw.Info);

			if (!IsPostBack) 
            {
                if ( !isNewItem ) 
                {
                    // Obtain a single row of MediaPlayerUrl information
					SqlDataReader dr                =   null;
					try
					{
		                if ( PageTraceSwitch.Sw.TraceVerbose )
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit Page_Load DesktopText ItemID: {0}.",ItemID),PageTraceSwitch.Sw.Info);
						dr                          =   MediaPlayerUrlDB.GetSingleMediaPlayerUrlGlobalized(ItemID);

						// Load first row into DataReader
						if (dr.Read())
						{	
   							TbCategory.Text             =   (string)dr["MDPCategory"];
   							TbTitle.Text                =   (string)dr["MDPTitle"];
			                if ( PageTraceSwitch.Sw.TraceVerbose )
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit Page_Load DesktopText MDPTitleField: {0}.",TbTitle.Text),PageTraceSwitch.Sw.Info);
                            object ob                   =   dr["MDPUrl"];
                            if ( ob != null && !DBNull.Value.Equals(ob) )
                            {
                                string imageStr         =   (string)ob;
                                if ( imageStr.StartsWith(UploadDirectory) )
                                    imageStr            =   imageStr.Substring(UploadDirectory.Length);
                                MDPUrlField.Text        = (string)imageStr;
                            }
			                if ( PageTraceSwitch.Sw.TraceVerbose )
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit Page_Load DesktopText MDPUrlField: {0}.",MDPUrlField.Text),PageTraceSwitch.Sw.Info);
                            ob                          =   dr["MDPDescription"];
                            if ( ob != null && !DBNull.Value.Equals(ob) )
							    DesktopText.TextContent =   (string)ob;
			                if ( PageTraceSwitch.Sw.TraceVerbose )
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit Page_Load DesktopText DesktopText.TextContent: {0}.",DesktopText.TextContent),PageTraceSwitch.Sw.Info);
                            TbLength.Value              =   (double)(int)dr["MDPLength"];
                            int type2                   =   (int)dr["MDPType2"];
                            RadComboBoxItem it          =   CbType2.Items.FindItemByValue(type2.ToString());
                            if ( it == null ) // not found we add it
                            {
                                it                      =   new RadComboBoxItem(string.Format("Unknown-{0}",type2),type2.ToString());
                                CbType2.Items.Add(it);
                            }
                            it.Selected                 =   true;
                            ob                          =   dr["MDPType"];
                            if ( ob != null && !DBNull.Value.Equals(ob) )
							    TbType.Text           =   (string)ob;
                            string fmt                  =   LocalizeHelper.Localize("MEDIAPLAYER_CREATE_MODIF_FMT","Created on {0:g}, Modified on {1:g}");
    					    CreatedDate.Text            =   string.Format(fmt,dr["MDPCreatedDate"],dr["MDPModifiedDate"]);
			                if ( PageTraceSwitch.Sw.TraceVerbose )
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit Page_Load DesktopText MDPCreatedDate: {0}.",CreatedDate.Text),PageTraceSwitch.Sw.Info);
						}
					}
					catch (Exception ex)
					{
            			if ( PageTraceSwitch.Sw.TraceError )
						    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibEdit Page_Load  ex : {0}", ex),PageTraceSwitch.Sw.Info);
                        throw;
					}
					finally
					{
						if (dr != null)
							dr.Close();
					}
                }
            }
        }

        /// <summary>
        /// create or update a MediaPlayerUrl.  It  uses the AddonNice.MediaPlayerLibDB()
        /// data component to encapsulate all data functionality.
        /// </summary>
        void MediaPlayerLibEdit_Update(object sender, AddonNice.UI.ADNPageEventArgs e)
        {
			if ( PageTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit MediaPlayerLibEdit_Update BEG  isNewItem: {0}", isNewItem),PageTraceSwitch.Sw.Info);
			try
			{
                int len     =   TbLength.Value.HasValue ? Convert.ToInt32(TbLength.Value.Value):0;
                int type2   =   0;
                int.TryParse(CbType2.SelectedValue,out type2);
                string imageStr         =   MDPUrlField.Text.Trim();
                if ( imageStr.StartsWith(UploadDirectory) )
                    imageStr            =   imageStr.Substring(UploadDirectory.Length);
				if ( isNewItem ) 
				{
					// Add the MediaPlayerUrl within the MediaPlayerLib table
					MediaPlayerUrlDB.AddMediaPlayerUrl(ModuleID, ItemID, TbCategory.Text,len,TbType.Text,type2,
                        TbTitle.Text,DesktopText.TextContent, imageStr);
				}
				else 
				{
					// Update the MediaPlayerUrl within the MediaPlayerLib table
					MediaPlayerUrlDB.UpdateMediaPlayerUrlGlobalized(ModuleID, ItemID, TbCategory.Text,len,TbType.Text,type2,
                        TbTitle.Text, DesktopText.TextContent, imageStr);
				}
			}
			catch(Exception ex )
			{
    			if ( PageTraceSwitch.Sw.TraceError )
				    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibEdit MediaPlayerLibEdit_Update  ex: {0}", ex),PageTraceSwitch.Sw.Info);
				throw;
			}
        }

        /// <summary>
        /// Delete a MediaPlayerUrl.  It  uses the AddonNice.MediaPlayerLibDB()
        /// data component to encapsulate all data functionality.
        /// </summary>
        void  MediaPlayerLibEdit_Delete(object sender, AddonNice.UI.ADNPageEventArgs e)
        {
			if ( PageTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MediaPlayerLibEdit MediaPlayerLibEdit_Delete BEG  isNewItem: {0}", isNewItem),PageTraceSwitch.Sw.Info);
			// Only attempt to delete the item if it is an existing item
            if ( !isNewItem ) 
            {
				try
				{
					MediaPlayerUrlDB.DeleteMediaPlayerUrlGlobalized(ItemID);
				}
				catch(Exception ex )
				{
    			    if ( PageTraceSwitch.Sw.TraceError )
				        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibEdit MediaPlayerLibEdit_Delete ex : {0}", ex),PageTraceSwitch.Sw.Info);
					throw;
				}

            }
        }

		#region Std Overrides

        /// <summary>
        /// Here we create the command buttons and add them to the PlaceHolders prepared
        /// </summary>
        override protected void OnPreInit(EventArgs e)
        {
            if ( PageTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine("[V]MediaPlayerLibEdit OnPreInit Begin",PageTraceSwitch.Sw.Info);
            if ( isNewItem ) 
                PageHasDeleteBtn    =   false;
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FAQEdit OnPreInit PageHasDeleteBtn: {0}.",PageHasDeleteBtn),PageTraceSwitch.Sw.Info);
            base.OnPreInit(e);
            if ( PageTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine("[V]MediaPlayerLibEdit OnPreInit End",PageTraceSwitch.Sw.Info);
        }

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }


        private void InitializeComponent() 
        {    
			if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]MediaPlayerLibEdit InitializeComponent BEG.",PageTraceSwitch.Sw.Info);
			Load	+=  new EventHandler(Page_Load);
            Update  +=  new PageEventHandler(MediaPlayerLibEdit_Update);
            Delete  +=  new PageEventHandler(MediaPlayerLibEdit_Delete);
			if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]MediaPlayerLibEdit InitializeComponent END.",PageTraceSwitch.Sw.Info);
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get
			{ return PlaceHolderButtons; }
			set { }
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get
			{ return PlaceHolderButtons2; }
			set { }
		}
		#endregion

    }
}

